package com.zhanyf.heilan.cg.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * LoginController负责打开登录页面(GET请求)和登录出错页面(POST请求)，
 * 
 * 真正登录的POST请求由Filter完成,
 * 
 * @author calvin
 */
@Controller
public class LoginController {

	@RequestMapping(value = "/doLogin", method = RequestMethod.POST)
	public String doLogin(String username, String password) {
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		try {
			SecurityUtils.getSubject().login(token);
			return "redirect:index";
		} catch (AuthenticationException e) {
			e.printStackTrace();
			return "account/login";
		}
	}

	@RequestMapping(value = "/login")
	public String login() {
		return "account/login";
	}

}
